package com.zhizhi.handle;

import com.zhizhi.common.constant.ResponseCodeConst;
import com.zhizhi.domain.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 全局异常处理 <br>
 * @date: 2020/7/22 10:45 <br>
 * @author: Mr.Yu
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    //请求方式异常
    @ResponseBody
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseDTO HttpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e) {
        log.error("error", e);
        return ResponseDTO.methodNotSupport();
    }

    //参数异常
    @ResponseBody
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseDTO HttpMessageNotReadableExceptionHandler() {
        return ResponseDTO.badRequest();
    }

    // 参数类型错误
    @ResponseBody
    @ExceptionHandler(TypeMismatchException.class)
    public ResponseDTO TypeMismatchExceptionHandler(TypeMismatchException e) {
        log.error("error", e);
        return ResponseDTO.wrap(ResponseCodeConst.BAD_REQUEST);
    }

    //参数校验未通过
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseDTO TypeMismatchExceptionHandler(MethodArgumentNotValidException e) {
        log.error("error", e);
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        List<String> msgList = fieldErrors.stream().map(FieldError::getDefaultMessage).collect(Collectors.toList());
        return ResponseDTO.wrap(ResponseCodeConst.BAD_REQUEST, String.join("；", msgList));
    }

    //参数空值异常
    @ResponseBody
    @ExceptionHandler(ParamException.class)
    public ResponseDTO ParamExceptionHandler(Exception e) {
        log.error("error", e);
        return ResponseDTO.paramsError(203, ((ParamException) e).getMsg());
    }

    @ResponseBody
    @ExceptionHandler(value = AuthorizationException.class)
    public Map<String, String> handleException(AuthorizationException e) {
        //e.printStackTrace();
        Map<String, String> result = new HashMap<String, String>();
        result.put("status", "400");
        //获取错误中中括号的内容
        String message = e.getMessage();
        String msg=message.substring(message.indexOf("[")+1,message.indexOf("]"));
        //判断是角色错误还是权限错误
        if (message.contains("role")) {
            result.put("msg", "对不起，您没有" + msg + "角色");
        } else if (message.contains("permission")) {
            result.put("msg", "对不起，您没有" + msg + "权限");
        } else {
            result.put("msg", "对不起，您的权限有误");
        }
        return result;
    }

    //未知异常
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseDTO ExceptionHandler(Exception e) {
        log.error("error", e);
        return ResponseDTO.badRequest();
    }


}
